package com.juzipi.demo.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {



    @GetMapping
    public String toIndex(Model model){
        model.addAttribute("msg","hello,Shiro!!!");
        return "index";
    }

    @RequestMapping("user/add")
    public String add(){

        return "user/add";
    }

    @RequestMapping("user/update")
    public String update(){

        return "user/update";
    }

    @RequestMapping("tologin")
    public String tologin(){

        return "login";
    }


    @RequestMapping("login")
    public String login(String username, String password,Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登陆数据
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);

        try {
            subject.login(usernamePasswordToken);//执行登录的方法
            return "index";
        } catch (UnknownAccountException uae) {
            model.addAttribute("msg","没有用户名为： " + usernamePasswordToken.getUsername());
            return "login";
        } catch (IncorrectCredentialsException ice) {
            model.addAttribute("msg","帐户密码 " + usernamePasswordToken.getPassword() + " 不正确！");
            return "login";
        } catch (LockedAccountException lae) {
            model.addAttribute("msg","用户名帐户 " + usernamePasswordToken.getHost() + " 被锁住了。  " +
                    "请与您的管理员联系以将其解锁。");
            return "login";
        }


    }


//    @GetMapping("user")
//    public User queryUser(User user){
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username",user.getUsername()).eq("password",user.getPassword());
//        return userMapper.selectOne(queryWrapper);
//    }







}
